//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<RenderSet.cpp>
///	@path	~/src/graphics/
///	@date	2008/04/27
///	@desc	.

#include "config/config.h"

#include "graphics/RenderSet.h"
#include "graphics/batch/CMainBatch.h"

namespace xeres {

	// RenderSet
	RenderSet::RenderSet( void )
	{
		for( size_t i = 0 ; i < RenderSet_last ; ++i )
			m_batches[i] = NULL;
	}
	// ~RenderSet
	RenderSet::~RenderSet( void )
	{
		for( size_t i = 0 ; i < RenderSet_last ; ++i )
			m_batches[i] = NULL;
	}
	// Initialize
	void RenderSet::Initialize( void )
	{
		RenderContext::Initialize();
		m_batches[RenderSet_Scene] = new CMainBatch;
	}
	// Finalize
	void RenderSet::Finalize( void )
	{
		RenderContext::Finalize();
		for( size_t i = 0 ; i < RenderSet_last ; ++i )
			m_batches[i] = NULL;
	}
	// BeginBatch
	void RenderSet::BeginBatch( void )
	{
		m_batches[RenderSet_Scene]->BeginBatch();
	}
	// EndBatch
	void RenderSet::EndBatch( void )
	{
		m_batches[RenderSet_Scene]->EndBatch();
	}
	// Render
	void RenderSet::Render( uint32 sets )
	{
		if( sets == RenderSet_Scene )
		{
			// render all
			m_batches[RenderSet_Scene]->Render( *this );
		}
		else
		{
			for( size_t i = RenderSet_Scene + 1 ; i < RenderSet_last ; ++i )
			{
				if( ( sets & ( 1 << i ) ) != 0 )
					if( m_batches[i].IsValid() )
						m_batches[i]->Render( *this );
			}
		}
	}
	// BatchDrawCall
	void RenderSet::BatchDrawCall( const function<void(RenderContext&)>& dc )
	{
		RefWeak<CMainBatch> batch = m_batches[RenderSet_Scene];
		batch->BatchDrawCall( dc );
	}
	// BatchModel
	void RenderSet::BatchModel( RefWeak<IModel> model )
	{
		RefWeak<CMainBatch> batch = m_batches[RenderSet_Scene];
		batch->BatchModel( model );
	}

} // namespace xeres
